import { ajax } from "../../utils/ajax";
const form = document.forms[0];
const point = document.getElementById('point');
const point2 = document.getElementById('point2');
//注册表单提交事件
form.onsubmit = form_submit;
form.username.onchange = username_change;
//注册表单提交处理函数
function form_submit(e) {
    e.preventDefault();
    if (password_change()) {
        ajax.post('post', '/api/admin/reg', new FormData(reg), localStorage.getItem('token')).then(result => {
            if (result.flag) {
                alert(result.msg);
                location.replace('../../regAdmin.html');
            } else {
                alert(result.msg);
                form.reset();
            }
        })
    } else {
        alert('用户名或密码输入错误')
    }
}
//注册用户名重名验证
function username_change() {
    ajax.get('get', '/api/admin/regUsername', { username: form.username.value }, localStorage.getItem('token')).then(result => {
        if (result.flag) {
            point.innerText = result.msg;
            point.style.color = 'rgb(102, 118, 102)';
        } else {
            point.style.color = 'red';
            point.innerText = result.msg;
        }
    })
}
//注册密码格式验证
form.password.onchange = password_change;
function password_change() {
    const pwdReg = /^.{3,10}$/g;
    if (pwdReg.test(form.password.value)) {
        point2.innerText = '格式正确';
        point2.style.color = 'rgb(102, 118, 102)';
        return true;
    } else {
        point2.style.color = 'red';
        point2.innerText = '请输入3~10个字符';
        return false;
    }
}